package ynu;

import java.io.*;
import java.util.*;

public class Theater {
    private String name;
    private int rowCount;
    private int seatCountPerRow;

    public Theater(String name, int rowCount, int seatCountPerRow) {
        this.name = name;
        this.rowCount = rowCount;
        this.seatCountPerRow = seatCountPerRow;
    }

    // 读取所有放映厅信息
    public static List<Theater> readTheaters() {
        List<Theater> theaters = new ArrayList<>();
        File file = new File("D:\\java实训大作业\\放映厅信息.txt");

        if (!file.exists()) {
            theaters.add(new Theater("黄金厅", 8, 10));
            theaters.add(new Theater("白银厅", 10, 12));
            theaters.add(new Theater("翡翠厅", 7, 8));
            theaters.add(new Theater("钻石厅", 7, 8));
            theaters.add(new Theater("宝石厅", 8, 9));
            saveTheaters(theaters);
            return theaters;
        }

        try (BufferedReader br = new BufferedReader(new FileReader(file))) {
            String line;
            while ((line = br.readLine()) != null) {
                String[] parts = line.split(",");
                if (parts.length == 3) {
                    theaters.add(new Theater(parts[0], Integer.parseInt(parts[1]),
                            Integer.parseInt(parts[2])));
                }
            }
        } catch (IOException e) {
            System.out.println("读取放映厅信息出错: " + e.getMessage());
        }
        return theaters;
    }

    // 保存所有放映厅信息
    public static void saveTheaters(List<Theater> theaters) {
        File dir = new File("D:\\java实训大作业");
        if (!dir.exists()) dir.mkdirs();

        try (BufferedWriter bw = new BufferedWriter(new FileWriter(
                "D:\\java实训大作业\\放映厅信息.txt"))) {
            for (Theater theater : theaters) {
                bw.write(theater.name + "," + theater.rowCount + "," + theater.seatCountPerRow);
                bw.newLine();
            }
        } catch (IOException e) {
            System.out.println("保存放映厅信息出错: " + e.getMessage());
        }
    }

    // 根据名称获取放映厅
    public static Theater getByName(String name) {
        return readTheaters().stream()
                .filter(t -> t.name.equals(name))
                .findFirst()
                .orElse(null);
    }

    // 初始化座位图（二维数组）
    public char[][] initSeatMap() {
        char[][] seatMap = new char[rowCount][seatCountPerRow];
        for (int i = 0; i < rowCount; i++) {
            for (int j = 0; j < seatCountPerRow; j++) {
                seatMap[i][j] = 'O';
            }
        }
        return seatMap;
    }

    // 序列化座位图
    public static String serializeSeatMap(char[][] seatMap) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < seatMap.length; i++) {
            sb.append(new String(seatMap[i]));
            if (i < seatMap.length - 1) sb.append(";");
        }
        return sb.toString();
    }

    // 反序列化座位图
    public static char[][] deserializeSeatMap(String seatStatus, int rows, int cols) {
        char[][] seatMap = new char[rows][cols];
        String[] rowStrings = seatStatus.split(";");

        for (int i = 0; i < rows; i++) {
            char[] row = (i < rowStrings.length && rowStrings[i].length() == cols) ?
                    rowStrings[i].toCharArray() : new char[cols];
            Arrays.fill(row, 'O');  // 默认空闲

            if (i < rowStrings.length && rowStrings[i].length() >= cols) {
                System.arraycopy(rowStrings[i].toCharArray(), 0, seatMap[i], 0, cols);
            }
        }
        return seatMap;
    }

    // Getters
    public String getName() { return name; }
    public int getRowCount() { return rowCount; }
    public int getSeatCountPerRow() { return seatCountPerRow; }
}